﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DTOs;

namespace giftdeliveryservice.Controllers
{
    public class ShoppingCartController : Controller
    {
        //
        // GET: /ShoppingCart/

        public ActionResult Detail()
        {
            return View();
        }

        public ActionResult Summary()
        {
            SelectedInvoice.ShippingCost = 2;
            return View();
        }


        private InvoiceDTO _selectedInvoice = new InvoiceDTO();
        public InvoiceDTO SelectedInvoice
        {
            get
            {
                if (_selectedInvoice == null)
                {
                    _selectedInvoice = new InvoiceDTO();
                    Session["Invoice"] = _selectedInvoice;
                }

                if (_selectedInvoice.InvoiceId == "")
                {
                    try
                    {
                        _selectedInvoice = (InvoiceDTO)Session["Invoice"];
                    }
                    catch { }
                }
                return _selectedInvoice;
            }
        }

        [HttpPost]
        public ActionResult AddtoCart(string productId, int quantity)
        {
            //int qty = 1; //default is 1 unless pass in another value

            //if (Request["qty"] != null)
            //{
            //    qty = int.Parse(Request["qty"].ToString());
            //}
            if (!string.IsNullOrEmpty(productId))
            {
                SelectedInvoice.AddToInvoice(productId, quantity);
            }
           
            return AddToCartPartialView();
        }


        public ActionResult AddToCartPartialView()
        {
            // get number of shopping cart items and display 

            int count = 0;
            if (SelectedInvoice != null && SelectedInvoice.InvoiceItems != null)
            {
                count = SelectedInvoice.InvoiceItems.Count;
            }

            return PartialView("_cart", SelectedInvoice.InvoiceItems.Count);
        }

        public ActionResult EmptyCard(string viewfrom)
        {
            
            SelectedInvoice.EmptyCart();
            SelectedInvoice.ShippingCost = 0;
            Session["Invoice"] = SelectedInvoice;

            if (viewfrom == "summary")
            {
                return _SummaryViewPartialView();
            }

            return _CartViewPartialView();

            //string t = productId;
        }

        public ActionResult Remove(string viewfrom, string productId)
        {
            SelectedInvoice.RemoveProduct(productId);
            if (SelectedInvoice.InvoiceItems.Count == 0)
            {
                SelectedInvoice.ShippingCost = 0;
            }
            Session["Invoice"] = SelectedInvoice;

            if (viewfrom == "summary")
            {
                return _SummaryViewPartialView();
            }

            return _CartViewPartialView();
           
        }

        public ActionResult CheckoutForm()
        {
            return RedirectToAction("Checkout");
        }

        public ActionResult Checkout()
        {
            return View(SelectedInvoice);
        }


        [HttpPost]
        public ActionResult Checkout(InvoiceDTO selectedInvoice)
        {


            return RedirectToAction("Summary");
        }

        public ActionResult _CartViewPartialView()
        {

            return PartialView("_CartView", SelectedInvoice);
        }

        public ActionResult _SummaryViewPartialView()
        {

            return PartialView("_SummaryView", SelectedInvoice);
        }
    
    }
}
